package cn.iocoder.yudao.module.ad.effect.dal.mysql;

import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.module.ad.effect.dal.dataobject.AdEffectRecordDO;
import cn.iocoder.yudao.module.api.ad.effect.enums.AdEffectSceneEnum;
import cn.iocoder.yudao.module.api.ad.effect.enums.AdEffectTypeEnum;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 广告效果记录 Mapper
 *
 * @author 山野羡民（1032694760@qq.com）
 */
@Mapper
public interface AdEffectRecordMapper extends CoreMapper<AdEffectRecordDO> {

    default List<Long> selectTodayCompletePlanIdsByUserId(Long userId) {
        LocalDateTime todayStart = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
        List<AdEffectRecordDO> list = selectList(new CoreLambdaQueryWrapper<AdEffectRecordDO>()
                .select(AdEffectRecordDO::getPlanId)
                .eq(AdEffectRecordDO::getUserId, userId)
                .eq(AdEffectRecordDO::getScene, AdEffectSceneEnum.CRYPTOCURRENCY_INCREMENT_SIGN.getValue())
                .eq(AdEffectRecordDO::getType, AdEffectTypeEnum.TASK_SUBMIT.getValue())
                .between(AdEffectRecordDO::getCreateTime, todayStart, LocalDateTime.now()));
        if (list == null) {
            return null;
        }
        return list.stream().map(AdEffectRecordDO::getPlanId).distinct().toList();
    }

    default int selectExposureCountByPlanId(Long planId) {
        Long count = selectCount(new CoreLambdaQueryWrapper<AdEffectRecordDO>()
                .eq(AdEffectRecordDO::getPlanId, planId)
                .eq(AdEffectRecordDO::getType, AdEffectTypeEnum.EXPOSURE.getValue()));
        if (count == null) {
            return 0;
        }
        return count.intValue();
    }

}
